<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加教师信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
            padding: 20px;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form">

    <!-- 姓名 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text"
                   name="teacherName"
                   lay-verify="required"
                   lay-reqtext="姓名不能为空"
                   placeholder="请输入教师姓名"
                   class="layui-input">
        </div>
    </div>

    <!-- 学院 -->
    <div class="layui-form-item">
        <label class="layui-form-label">学院</label>
        <div class="layui-input-block">
            <input type="text"
                   name="institute"
                   placeholder="请输入学院"
                   class="layui-input">
        </div>
    </div>

    <!-- 职称 -->
    <div class="layui-form-item">
        <label class="layui-form-label">职称</label>
        <div class="layui-input-block">
            <input type="text"
                   name="type"
                   placeholder="请输入职称（如：副教授）"
                   class="layui-input">
        </div>
    </div>

    <!-- 性别 -->
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="text"
                   name="sex"
                   placeholder="请输入性别（男/女）"
                   class="layui-input">
        </div>
    </div>

    <!-- 电话 -->
    <div class="layui-form-item">
        <label class="layui-form-label">电话</label>
        <div class="layui-input-block">
            <input type="text"
                   name="tel"
                   lay-verify="phone"
                   placeholder="请输入手机号"
                   class="layui-input">
        </div>
    </div>

    <!-- 身份证号 -->
    <div class="layui-form-item">
        <label class="layui-form-label">身份证号</label>
        <div class="layui-input-block">
            <input type="text"
                   name="cardId"
                   lay-verify="idcard"
                   placeholder="请输入身份证号"
                   class="layui-input">
        </div>
    </div>

    <!-- 邮箱 -->
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text"
                   name="email"
                   lay-verify="email"
                   placeholder="请输入邮箱"
                   class="layui-input">
        </div>
    </div>

    <!-- 密码 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="password"
                   name="pwd"
                   lay-verify="required"
                   lay-reqtext="密码不能为空"
                   placeholder="请输入密码"
                   class="layui-input">
        </div>
    </div>

    <!-- 按钮 -->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>

<script src="../../../lib/layui-v2.6.3/layui.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    layui.use(['form', 'layer'], function () {
        const form = layui.form, layer = layui.layer, $ = layui.$;

        form.on('submit(saveBtn)', function (data) {
            const teacherData = {
                teacherName: data.field.teacherName,
                institute: data.field.institute,
                type: data.field.type,  // ✅ 注意字段名是 type
                sex: data.field.sex,
                cardId: data.field.cardId,
                tel: data.field.tel,
                email: data.field.email,
                pwd: data.field.pwd,
                role: '1'  // 教师角色
            };

            axios.post('/teacher', teacherData)
                .then(function (response) {
                    const res = response.data;
                    if (res.code === 200) {
                        layer.msg('添加教师成功！', {icon: 1, time: 1500}, function () {
                            const iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            if (parent.layui && parent.layui.table) {
                                parent.layui.table.reload('currentTableId');
                            }
                        });
                    } else {
                        layer.msg('添加失败：' + (res.msg || '未知错误'), {icon: 2, time: 2000});
                    }
                })
                .catch(function (error) {
                    layer.msg('请求失败：' + error.message, {icon: 2, time: 2000});
                });

            return false;
        });
    });
</script>
</body>
</html>
